package com.masterclient.app.businessmodule.settingmodule.bankcard;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.middlelib.router.intercept.FCBaseIntercept;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;

import com.masterclient.app.base.CashCommonDialog;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.settingmodule.bankcard.bean.NewBankBean;
import com.masterclient.app.businessmodule.settingmodule.bankcard.bean.NewBankCardBean;
import com.masterclient.app.businessmodule.settingmodule.bankcard.contract.BankCardManageContract;
import com.masterclient.app.businessmodule.settingmodule.bankcard.contract.BankCardManagePresenter;
import com.eoner.baselib.utils.status.StatusBarUtil;


import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;


public class BankCardManageActivity extends FCBusinessActivity<BankCardManagePresenter> implements BankCardManageContract.View{

    @BindView(R.id.none_card_layout)
    View none_card_layout;
    @BindView(R.id.card_layout)
    View card_layout;
    @BindView(R.id.bank_layout)
    View bank_layout;

    @BindView(R.id.bank_name)
    TextView bank_name;
    @BindView(R.id.bank_card_number)
    TextView bank_card_number;
    @BindView(R.id.update_card)
    TextView update_card;
    @BindView(R.id.unbind_card)
    TextView unbind_card;
    @BindView(R.id.non_use_txt)
    TextView non_use_txt;
    @BindView(R.id.anch0)
    ImageView anch0;

    private ConfirmDialog confirmDialog;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.bankcard_manage;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("银行卡管理");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        StatusBarUtil.setStatusBarColor(getActivity(), Color.parseColor("#FFFFFF"));
        confirmDialog = new ConfirmDialog(mContext,"解绑后将无法正常提现，需重新绑定银行卡，是否确认解绑","取消","确定");
        mPresenter.getSettingBankCardInfo();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case REFRESH_CARD:
                mPresenter.getSettingBankCardInfo();
                break;
            case 104:

                break;
        }
    }

    public static final int REFRESH_CARD = 2;

    @OnClick({R.id.none_card_layout,R.id.update_card,R.id.unbind_card})
    void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.none_card_layout:
                if(!UserDataManager.getInstance().getCheckIdCardStatus().equals("1")){
                   display_upload_ids_dialog();
                }else{
                    intent = new Intent(BankCardManageActivity.this,BankCardInsertActivity.class);
                    startActivityForResult(intent,REFRESH_CARD);
                }
                break;
            case R.id.update_card:
                if(!UserDataManager.getInstance().getCheckIdCardStatus().equals("1")){
                    display_upload_ids_dialog();
                }else{
                    intent = new Intent(BankCardManageActivity.this,BankCardInsertActivity.class);
                    intent.putExtra("bankCardData", (Serializable) bankCardData);
                    startActivityForResult(intent,REFRESH_CARD);
                }
                break;
            case R.id.unbind_card:
                confirmDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                    @Override
                    public void cancel() {
                        confirmDialog.dismiss();
                    }

                    @Override
                    public void confirm() {
                        mPresenter.bankRemove(bankCardData.bankcard.bankcardCertificationId);
                        confirmDialog.dismiss();
                    }
                });
                confirmDialog.show();
                break;
        }
    }

    CashCommonDialog ids_dialog;
    private void display_upload_ids_dialog() {
        if(ids_dialog == null){
            ids_dialog = new CashCommonDialog(mContext,"为保证您的资金账户安全,请您先绑定本人身份证信息再进行绑卡","取消","去实名",1);
        }
        ids_dialog.setOnChooseListerner(new CashCommonDialog.OnChooseListerner() {
            @Override
            public void cancel() {
                ids_dialog.dismiss();
            }

            @Override
            public void confirm() {
                ids_dialog.dismiss();

                ARouter.getInstance().build(FCRouterPath.SETTING_AUTH_PATH)
                        .navigation(BankCardManageActivity.this,new FCBaseIntercept());

            }
        });
        ids_dialog.show();
    }


    @Override
    public void getBankSuccess(NewBankBean data) {

    }

    @Override
    public void getBankFailed(String msg) {

    }

    @Override
    public void BankManageSuccess() {

    }

    @Override
    public void BankManageFailed(String msg) {

    }

    NewBankCardBean bankCardData;

    @Override
    public void showBankCard(NewBankCardBean data) {
        bankCardData = data;
        if(data != null && data.bankcard != null && !TextUtils.isEmpty(data.bankcard.bankcardNumber)){
            none_card_layout.setVisibility(View.GONE);
            card_layout.setVisibility(View.VISIBLE);
            update_card.setVisibility(View.VISIBLE);
            unbind_card.setVisibility(View.VISIBLE);
            card_layout.setBackgroundResource(R.drawable.cash_bankcard_layout_bkg);

            bank_name.setText(data.bankcard.bankName);
            bank_card_number.setText(data.bankcard.bankcardNumber);

            if("0".equals(data.bankcard.bankcardStatus)){
                //非本人身份证
                non_use_txt.setVisibility(View.VISIBLE);
                bank_name.setTextColor(Color.parseColor("#C2C5CC"));
                bank_card_number.setTextColor(Color.parseColor("#C2C5CC"));
                anch0.setImageResource(R.mipmap.bank_default_grey_icon);
            }else{
                non_use_txt.setVisibility(View.GONE);
                bank_name.setTextColor(Color.parseColor("#141922"));
                bank_card_number.setTextColor(Color.parseColor("#141922"));
                anch0.setImageResource(R.mipmap.bank_default_icon);
            }


        }else{
            none_card_layout.setVisibility(View.VISIBLE);
            card_layout.setVisibility(View.GONE);
            update_card.setVisibility(View.GONE);
            unbind_card.setVisibility(View.GONE);
            card_layout.setBackgroundResource(R.drawable.bankcard_manage_none_bkg);
        }

    }

    @Override
    public void getBankCardFail(String msg) {
        none_card_layout.setVisibility(View.VISIBLE);
        card_layout.setVisibility(View.GONE);
        update_card.setVisibility(View.GONE);
        unbind_card.setVisibility(View.GONE);
        card_layout.setBackgroundResource(R.drawable.bankcard_manage_none_bkg);
    }

    @Override
    public void BankRemoveSuccess() {
        mPresenter.getSettingBankCardInfo();
    }

    @Override
    public void BankRemoveFailed(String msg) {
          showToast(msg);
    }

    @Override
    public void getCaptchaSuccess() {

    }

    @Override
    public void getCaptchaFailed() {

    }

    @Override
    public void getCaptchaFailed(String msg) {

    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getSettingBankCardInfo();
    }
}
